package com.example.su_shi_tan_bas_lib.view.JssAdvTextSwitcher;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.example.su_shi_tan_bas_lib.R;
import com.example.su_shi_tan_bas_lib.global.AppGlobals;


public class JssAdvTextSwitcher extends TextSwitcher {

    private Context mContext;
    private String[] mTexts = {};
    private int currentPos;
    private Callback mCallback = new Callback() {
        @Override
        public void onItemClick(int position) {
        }
    };

    public JssAdvTextSwitcher(Context context) {
        super(context);
    }

    public JssAdvTextSwitcher(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.mContext = context;
        int[] attrsArray = new int[]{
                android.R.attr.textColor,
                R.attr.textSize,
                android.R.attr.inAnimation,
                android.R.attr.outAnimation,
                R.attr.gravity
        };
        TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);

        final int textColor = ta.getColor(0, Color.BLACK);

        final float textSize = ta.getDimension(1, 20f);


        final int animInRes = ta.getResourceId(2, R.anim.fade_in_slide_in);
        final int animOutRes = ta.getResourceId(3, R.anim.fade_out_slide_out);
        boolean right = (ta.getInt(4, 0) & 0x01) == 0x01;
        boolean left = (ta.getInt(4, 0) & 0x02) == 0x02;
        boolean center = (ta.getInt(4, 0) & 0x03) == 0x03;
        final int gravity = center ? Gravity.CENTER :
                (right ? Gravity.RIGHT | Gravity.CENTER_VERTICAL :
                        (left ? (Gravity.LEFT | Gravity.CENTER_VERTICAL)
                                : Gravity.NO_GRAVITY)
                );
        ta.recycle();
        this.setFactory(() -> {
            TextView innerText = new TextView(mContext);

            innerText.setGravity(gravity);

            innerText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

            innerText.setTextColor(textColor);

            innerText.setSingleLine(true);

            innerText.setMaxEms(16);

            innerText.setEllipsize(TextUtils.TruncateAt.MARQUEE);

            innerText.setHorizontallyScrolling(true);

            innerText.setMarqueeRepeatLimit(-1);

            innerText.setOnClickListener(p1 -> JssAdvTextSwitcher.this.onClick());
            return innerText;
        });

        Animation animIn = AnimationUtils.loadAnimation(mContext, animInRes);
        Animation animOut = AnimationUtils.loadAnimation(mContext, animOutRes);

        this.setInAnimation(animIn);
        this.setOutAnimation(animOut);
    }

    public int sp2px(float spValue) {
        final float fontScale = AppGlobals.getApplication().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public void overrideText(String text) {
        this.setText(text);
    }

    public void setAnim(int animInRes, int animOutRes) {
        Animation animIn = AnimationUtils.loadAnimation(mContext, animInRes);
        Animation animOut = AnimationUtils.loadAnimation(mContext, animOutRes);
        this.setInAnimation(animIn);
        this.setOutAnimation(animOut);
    }

    public void setAnim(Animation animIn, Animation animOut) {
        this.setInAnimation(animIn);
        this.setOutAnimation(animOut);
    }

    public void setTexts(String[] texts) {
        if (texts.length > 0) {
            this.mTexts = texts;
            this.currentPos = 0;
        }
        updateDisp();
    }

    public void setCallback(Callback callback) {
        this.mCallback = callback;
    }

    public void next() {
        if (mTexts.length > 0) {
            if (currentPos < mTexts.length - 1) {
                currentPos++;
            } else {
                currentPos = 0;
            }
            updateDisp();
        }
    }

    public void previous() {
        if (mTexts.length > 0) {
            if (currentPos > 0) {
                currentPos--;
            } else {
                currentPos = mTexts.length - 1;
            }
            updateDisp();
        }
    }

    public interface Callback {
        public void onItemClick(int position);
    }

    private void updateDisp() {
        this.setText(mTexts[currentPos]);
    }

    private void onClick() {
        mCallback.onItemClick(currentPos);
    }
}
